window.onload=function(){
    axios.defaults.withCredentials = true;
    //获取用户登录
    axios.get('https://elm.cangdu.org/v1/user').then(res => {
        console.log(res);
        if(res.data.status==0){
            $('#kh_headertaxt').html(`登录/注册`).click(function(){
                window.location.href=`/pages/Singin/singin.html`
            })
        }else{
            $('#kh_headertaxt').html(`<img src='/img/导航栏/个人.png' class='kh_gerentouxiang'>`).click(function(){
                window.location.href=`/pages/my/my.html`
        })
    }

    })

//自动定位
$ajax({
    url: 'https://elm.cangdu.org/v1/cities/240',
    method:'get',
    // Headers:`Access-Control-Allow-Origin`,
    // data:{
    //     type:'guess'
    // },
    success:(res)=>{ 
        console.log(res)
        let kg_nowgps=document.getElementById("kg_nowgps")
        
        kg_nowgps.innerHTML=`${res.name}`
        $('.kh_gpsnow').on('click',function(){
            window.location.href=`/pages/searchcity/searchcity.html?id=${res.id}`
        console.log(555);
        })
        }  
})
//获取热门城市
$ajax({
    url: 'https://elm.cangdu.org/v1/cities?type=hot',
    method:'get',

    success:(res)=>{ 
        console.log(res);
        // let kh_hotcity2=document.getElementsByClassName("kh_hotcity2")[0]
        for(let i=0;i<res.length;i++){
            $create('kh_hotcity2','kh_hotbox',`${res[i].name}`)  
        }
        let kh_hotbox=document.getElementsByClassName("kh_hotbox")
        for (let i = 0; i < res.length; i++) {
            kh_hotbox[i].addEventListener('click',function(){
                window.location.href=`/pages/searchcity/searchcity.html?id=${res[i].id}`
            })
            
        }
        
        }  
})

//获取全部城市
$().ajax({
    url: 'https://elm.cangdu.org/v1/cities?type=group',
    method:'get',

    success:(res)=>{ 
        console.log(res);
        let  kh_allcity=document.getElementsByClassName(" kh_allcity")[0]
        let cityClassifyArr = [];
        for(let c_item in res){
            cityClassifyArr.push(c_item);
        }
        let cityClassifyArrSort = cityClassifyArr.sort(function(m,n){
            let a = m.toLowerCase();
            let b = n.toLowerCase();
            if(a < b){
                return -1;
            }
            if(a > b){
                return 1;
            }else{
                return 0;
            }
        })
        for(let i in cityClassifyArrSort){
            let c = cityClassifyArrSort[i];
            let classifyWrap = $().create('div').addClass('kh_cityClassifyWrap');
            let title = $().create('div').addClass('kh_allcity1').html(c);
            let item_wrap = $().create('div').addClass('kh_citys');
            $(classifyWrap).append(title);
            $(classifyWrap).append(item_wrap);
            for(c_index in res[c]){
                let c_item = res[c][c_index];
                let item = $().create('div').addClass('kh_allcity2').html(c_item.name);
                $(item_wrap).append(item);
                item .click(function(){
                    window.location.href=`/pages/searchcity/searchcity.html?id=${c_item.id}`
                })

            }
            $(kh_allcity).append(classifyWrap);
        }
     
        
        }  
})




































}